home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / btb / btb.doc < prev    next >
Text File  |  1992-07-18  |  8KB  |  152 lines

  1.  
  2.                               GENERATION OF
  3.                       "SELF-EXTRACTING" BASIC FILES
  4.  
  5.                         by Giuliano Artico I3LGP
  6.  
  7.  
  8.  
  9.    1. Transformation of a binary file into a basic program.
  10.  
  11.    The problem of transforming a binary file into a form compatible with
  12. the trasmission via packet radio as a text file leaded to the production of
  13. several programs which perform such function in a more or less efficient
  14. way.
  15.    The primary requirement of a software of this type is reducing the size
  16. of the output file as most as possible to diminish the channel occupation
  17. during the transmission. For this reason, some programs employ
  18. compression algoritms whose complexity causes an augmentation of the size
  19. of the extracting routine, which therefore cannot be incorporated in the
  20. transmitted file.
  21.    Of course, the ideal capability would be reached if everyone would use
  22. the same encoding program, the one with the greatest efficacy:
  23. consequently, however, all potential users ought to retain such program in
  24. order to decode received texts.
  25.    Reaching such an aim seems to be quite difficult, since there exist
  26. several different programs with good characteristics. Therefore a simpler
  27. solution may sometimes provide satisfactory results: converting a binary file
  28. into a "self-extracting" program, which is divided into two parts, the
  29. decoding routine and the text to be converted. The easiest language is
  30. BASIC, but other ones might be adopted.
  31.    The BASIC program I propose in the present note generates a auto-
  32. rebuilding BASIC file, asking for a binary file to be processed in the
  33. encoding phase. Once the output file has been produced, it may be run
  34. (e.g., under GW-Basic) and the original binary file is reconstructed.
  35.    In writing the program, I estimated opportune to choose an encoding
  36. method which balances length of extracting routine with the total size of
  37. stored data. The very easiest method for the conversion is storing in the
  38. data section all the ASCII or hexadecimal codes of bytes picked up from
  39. the binary file: this allows decodification by means of a very low number of
  40. instructions, but the amount of spaceoccupied by data is between twice and
  41. three times the size of the original file.
  42.    Therefore I adopted the "six-bit" encoding method, which converts a
  43. sequence of three bytes into a sequence of four characters: consequently
  44. the resulting text is one third greater than the size of the given binary
  45. file. Also in this case some space is occupied by line numbers and sintax
  46. words.
  47.    To understand how the conversion algoritm works, consider that a
  48. sequence of three bytes may be represented as follows:
  49.  
  50.                        ******** ******** ********
  51.  
  52. where every '*' represents the position of a bit in its byte. We have a
  53. string of  24 bits, which may be splitted into four sequences of six bits:
  54.  
  55.                        ****** ****** ****** ******
  56.  
  57. Since the six positions of such a sequence may be occupied either by 0 or
  58. by 1, each of them allows 2^6=64 dispositions. The algoritm has just to
  59. establish a correspondence between every such a disposition and a
  60. character, taken among the ones that may be sent via packet radio in the
  61. text form, that is the ones whose ASCII code is between 32 and 126. I
  62. chose the character set ranging between # (ASCII 35) and b (ASCII 98) to
  63. skip " which is the string delimiter in BASIC.
  64.    I obtained such correspondence by using the logic operators AND and
  65. OR and the operations * and \, applied in a suitable way to the decimal
  66. ASCII code of input bytes. Every ASCII code is an integer number between
  67. 0 and 255 that may be represented as a sequence of eight digits 0 or 1. For
  68. instance, suppose you are given a byte whose ASCII code is 183 and
  69. observe the following table:
  70.  
  71.          183  =  1 0 1 1 0 1 1 1
  72.          252  =  1 1 1 1 1 1 0 0
  73.  183 AND 252  =  1 0 1 1 0 1 0 0  =  180
  74.  
  75. The third line contains the result of the operator AND applied to numbers
  76. 183 and 252: in every position we put the smallest between the two digits
  77. occupying the same position in the two superior lines. In other words, we
  78. have extracted the first six bits of the given byte binary representation by
  79. performing the operation AND between the given number and the number
  80. 252. Similarly, denoting the ASCII code by x, we may extract the first
  81. four bits of its binary representation by "x AND 240", the last four bits by
  82. "x AND 15", the last two bits with "x AND 3" and so on.
  83.    We have not space enough for further details. Just let's say that the
  84. operator OR allows to join together two previously extracted fragments of
  85. byte if they are disjoint from each other, that is if the operator AND
  86. applied to them gives 0 as result.
  87.    The aritmetic operations * (multiplication) and \ (integer division) are
  88. used to "shift" a "piece of byte" towards left side and right side
  89. respectively.
  90.  
  91.    2. Description of the code.
  92.  
  93.    BTB.BAS (BinToBas) and files generated by it may either be run under
  94. GW-Basic or be compiled with Quick Basic or Bascom.
  95.    I suggest to use a compiled version since it allows to enter input file
  96. and output file names as parameters on the DOS command line when BTB is
  97. run. Furthermore, the syntax of the command line and a short help may be
  98. also obtained by typinga '?' as argument at the DOS prompt.
  99.    Notice that in compiling BTB it is necessary to invoke options /E (ON
  100. ERROR) and /X (RESUME).
  101.    Essentially, the code consists of sub-routines 2000, 3000, 6000 and 7000.
  102.    Sub-routine 2000. If the variable COMMAND$ is different from the null
  103. string, then the first two parameters are extracted from it. The first
  104. parameter is assumed as the name of the source binary file (input file) and
  105. the second one as the name of the target file (basic program).
  106.    Sub-routine 3000. If the source file name has not been entered on the
  107. DOS command line, then the program asks for it. Subsequently, if the
  108. target file name is missing, then it is obtained from the name of the source
  109. file by replacing its extension with ".BTB" (routine 5000). Then the program
  110. checks the existance of both files. If the binary file does not exist then a
  111. new name is requested but if the name has been supplied at the DOS
  112. prompt then the control is returned to DOS. If the target file exists then a
  113. question is put wether to overwrite it or not, to guard against its deletion.
  114.    Sub-routine 6000. It writes to the output file the extracting routine,
  115. which will re-build the original binary file. Such routine performs some
  116. controls which are described below.
  117.    Sub-routine 7000. This is the encoding phase: strings of 51 bytes are
  118. taken out from the input file and converted to a text line of 68 characters
  119. with the procedure delineated above. A check sum is computed for every
  120. line and converted to a byte which is put at the end of the line and then
  121. every line is stored in the target file. A total check sum is calculated and
  122. written as the last data line of the file.
  123.  
  124.    3. Files generated by BINTOBAS.
  125.  
  126.    The extracting routine consists of the first 20 lines of BASIC files
  127. generated by BINTOBAS. Its total length is 1077 bytes. It verifies the
  128. existance of a file with the name that is stored at line 20 and exits if
  129. such file is present. 
  130.    Some controls are performed to ensure that the resulting binary file
  131. matches the original one:
  132.    a) the size of binary file and the number of data lines must be
  133. coherent with each other: also this values are contained in line 20;
  134.    b) length of data strings must be greater than or equal to 69 (all
  135. characters following the 69th are skipped);
  136.    c) the 69th byte in every DATA string (except the last one) is a "local
  137. parity control" which is used to ensure the correctness of that string;
  138.    d) character ASCII code must be in the correct range (ASCII 35 to 98);
  139.    e) finally, check sum must be identical to the one stored in the last
  140. data line.
  141.    If any of the previous conditions is not satisfied, the routine exits with
  142. an error message.
  143.    The resulting file is identic to the original one: no <CTRL-Z> character
  144. is added at the end, since a random access file is used for output.
  145.  
  146. Address: Giuliano Artico
  147.          Dipartimento di Matematica Pura e Applicata
  148.          Via Belzoni 7, 35131 Padova, Italy
  149. Phone  : 049 831909
  150. E-mail : Internet = ARTICO@PDMAT1.UNIPD.IT    Decnet = 38999::ARTICO
  151.  
  152.